/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.crayon.account.impl.OnSegmentRadioChange;
import com.crayon.account.impl.OnSegmentRadioChange.SEGMENTMENU;
import com.crayon.sharemenu.R;
import com.makeramen.segmented.CenteredRadioImageButton;
import com.makeramen.segmented.SegmentedRadioGroup;

import java.util.List;

/**
 * com.crayon.account.fragments.FragmentSegmentButtom
 * @author kws
 *
 */
public class FragmentSegmentButtom extends Fragment
implements RadioGroup.OnCheckedChangeListener{
    OnSegmentRadioChange onSegmentRadioChange;
	SegmentedRadioGroup segmentedRadioGroup;
	OnSegmentMenuItemCreater onSegmentMenuItemCreater;
	LayoutInflater inflater;
	public static FragmentSegmentButtom onCreaetInstance(String[] args){
		FragmentSegmentButtom f = new FragmentSegmentButtom();
		Bundle bundle =new Bundle();
	
		bundle.putStringArray("items", args);
		
		return f;
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.inflater = inflater;
		View rootView = inflater.inflate(R.layout.fragment_segment_buttom,container, false);
		
		segmentedRadioGroup = (SegmentedRadioGroup)rootView.findViewById(R.id.segment_rgdioGroupd);
        for(int i=0;i < segmentedRadioGroup.getChildCount();i++){
            View view = segmentedRadioGroup.getChildAt(i);
            view.setVisibility(View.GONE);
        }
		if(getOnSegmentMenuItemCreater()!=null){
//			segmentedRadioGroup.removeAllViews();
			List<SEGMENTMENU> itemArray = getOnSegmentMenuItemCreater().OnBindListItems();
			for(int i = 0; i < itemArray.size() ; i++){
				SEGMENTMENU item = itemArray.get(i);
				CenteredRadioImageButton imageButton=null;
				if(i < segmentedRadioGroup.getChildCount()){
					imageButton = (CenteredRadioImageButton)segmentedRadioGroup.getChildAt(i);
				}else{
					imageButton = (CenteredRadioImageButton)inflater.inflate(R.layout.layout_segment_centerdimagebutton, null);
					segmentedRadioGroup.addView(imageButton);
				}
                imageButton.setVisibility(View.VISIBLE);
				imageButton.setId(item.getnId());
				imageButton.setText(item.getnId());
                if(i==0) segmentedRadioGroup.check(item.getnId());
			}


			segmentedRadioGroup.changeButtonsImages();
			segmentedRadioGroup.invalidate();
		}
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		segmentedRadioGroup.setOnCheckedChangeListener(this);
	}
	public void setCheckedRadioButtonId(int id){
		segmentedRadioGroup.check(id);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		if(getOnSegmentRadioChange()!=null){
			if(getOnSegmentMenuItemCreater()!=null){
				for(SEGMENTMENU name : SEGMENTMENU.values()){
					if(checkedId==name.getnId()){
						getOnSegmentRadioChange().onChangedMenu(name);
						return;
					}
				}
			}
			switch(checkedId){
			case R.id.button_category:
				getOnSegmentRadioChange().onChangedMenu(SEGMENTMENU.MONTHLYCATEGORY);
				break;
			case R.id.button_cardbank:
				getOnSegmentRadioChange().onChangedMenu(SEGMENTMENU.MONTHLYCARDBANK);
				break;
			case R.id.button_add:
			default:
				getOnSegmentRadioChange().onChangedMenu(SEGMENTMENU.MONTHLYLIST);
				break;
			}
		}
	}

	public OnSegmentRadioChange getOnSegmentRadioChange() {
		return onSegmentRadioChange;
	}

	public void setOnSegmentRadioChange(OnSegmentRadioChange onSegmentRadioChange) {
		this.onSegmentRadioChange = onSegmentRadioChange;
	}
	public OnSegmentMenuItemCreater getOnSegmentMenuItemCreater() {
		return onSegmentMenuItemCreater;
	}
	public void setOnSegmentMenuItemCreater(
			OnSegmentMenuItemCreater onSegmentMenuItemCreater) {
		this.onSegmentMenuItemCreater = onSegmentMenuItemCreater;
	}


	

}
